package yawning.mew.cat;

import yawning.mew.cat.CatModelSimulation.SimulatorProperty;
import yawning.mew.character.CharacterModel.AttackResult;
import yawning.mew.sim.SimulationAction;
import yawning.mew.sim.SimulationEventAction;

public class EventFeralFF extends SimulationEventAction {
	CatModelSimState mState;
	boolean mExternal;
	int mStacks = 0;
	double mCooldownExpireTime = 0.0;

	EventFeralFF(CatModelSimState state) {
		super(state.mSimUtil);
		mState = state;
		mExternal = mState.mSimUtil.getConfig().getBool(SimulatorProperty.ASSUMEEXTERNALBLEED);
		if (mExternal) {
			mStacks = 3;
			schedule(Double.MAX_VALUE);
		}
		mState.mModel.calculateTargetArmor(mStacks, false);
	}

	@Override
	public double perform() {
		if (mCooldownExpireTime <= mState.mSimUtil.getSimTime()) {
			if (!mExternal) {
				AttackResult result = mState.mModel.getSpellResult();

				if (mState.mDebug) mState.DBGF("Feral FF (%s)", result);

				switch (result) {
				case MISS:
					mState.nrFFMiss++;
					break;
				case HIT:
				case CRIT:
					mState.nrFFHit++;

					// Recalculate armor here.
					mStacks = Math.min(3, mStacks + mState.mModel.nrFeralFFStacksPerCast);
					mState.mModel.calculateTargetArmor(mStacks, mState.isShatteringThrowUp());
					schedule(mState.mModel.feralFFDebuffDuration);

//					if (mDebug) DBGF("  Expires at: %f", feralFFEv.getExpiryTime());

					break;
				}
			}

			mCooldownExpireTime = mState.mSimUtil.getSimTime() + mState.mModel.feralFFCDDuration;
			return 1.0;
		}
		return SimulationAction.ACTION_FAIL;
	}

	@Override
	public void onExpire() {
		if (mState.mDebug) mState.DBGF("Feral FF Expiration");
		mStacks = 0;
		mState.mModel.calculateTargetArmor(mStacks, mState.isShatteringThrowUp());
	}
}